package com.wanxin.wb.common.support;



import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;


import com.wanxin.wb.common.support.exception.CodeDesc;
import com.wanxin.wb.common.support.json.IncludePropertyFilter;
import com.wanxin.wb.common.support.debug.DebugResult;
import com.wanxin.wb.common.support.debug.DebugUtil;

import java.util.List;

/**
 * <B>Description:</B> <br>
 * <B>Create on:</B> 2016/9/19<br>
 *
 * @author 
 * @version 1.0
 */
public class ResultUtil {
    public static final String TRUE_STRING  = "1";
    public static final String FALSE_STRING = "0";

    /**
     * <B>Description:</B>返回json结果 <br>
     * <B>Create on:</B> 2015年4月2日 上午10:32:52<br>
     *
     * @param data    返回的结果
     * @param filters 用于对json输出结果进行控制
     * @return
     * @author 
     */
    public static String success(Object data, PropertyFilter... filters) {
        Result result = null;
        if (!DebugUtil.getDebugInfo().isEnable()) {
            result = new Result();
        } else {
            result = new DebugResult();
        }
        result.setSuccess(TRUE_STRING);
        result.setSn(ServiceNumberSupport.getServiceNumber());
        result.setData(data);
        SerializeWriter sw = new SerializeWriter();
        JSONSerializer serializer = new JSONSerializer(sw);
        if (filters != null && filters.length > 0) {
            for (PropertyFilter filter : filters) {
                if (filter != null) {
                    serializer.getPropertyFilters().add(filter);
                }
            }
        }

        /**add by zhaopan 2016.11.25 15:30 处理循环引用输出$ref等问题*/
        serializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
        serializer.config(SerializerFeature.WriteMapNullValue, true);

        serializer.write(result);
        return sw.toString();
    }

    public static String success(Object data, String isSuccess, PropertyFilter... filters) {
        Result result = null;
        if (!DebugUtil.getDebugInfo().isEnable()) {
            result = new Result();
        } else {
            result = new DebugResult();
        }
        result.setSuccess(isSuccess);
        result.setSn(ServiceNumberSupport.getServiceNumber());
        result.setData(data);
        SerializeWriter sw = new SerializeWriter();
        JSONSerializer serializer = new JSONSerializer(sw);
        if (filters != null && filters.length > 0) {
            for (PropertyFilter filter : filters) {
                if (filter != null) {
                    serializer.getPropertyFilters().add(filter);
                }
            }
        }

        serializer.config(SerializerFeature.WriteMapNullValue, true);

        serializer.write(result);
        return sw.toString();
    }

    public static String fail(CodeDesc codeDesc) {
        return fail(codeDesc, null);
    }

    public static String fail(CodeDesc codeDesc, Object data, PropertyFilter... filters) {
        Result result = null;
        if (!DebugUtil.getDebugInfo().isEnable()) {
            result = new Result();
        } else {
            result = new DebugResult();
        }
        result.setSuccess(FALSE_STRING);
        result.setSn(ServiceNumberSupport.getServiceNumber());
        result.setData(data);
        if (codeDesc != null) {
            result.setCode(codeDesc.getCode());
            result.setMsg(codeDesc.getMsg());
        }
        SerializeWriter sw = new SerializeWriter();
        JSONSerializer serializer = new JSONSerializer(sw);
        if (filters != null && filters.length > 0) {
            for (PropertyFilter filter : filters) {
                if (filter != null) {
                    serializer.getPropertyFilters().add(filter);
                }
            }
        }

        serializer.config(SerializerFeature.WriteMapNullValue, true);

        serializer.write(result);
        return sw.toString();
    }

    /**
     * <B>Description:</B> 生成IncludePropertyFilter<br>
     * <B>Create on:</B> 2015年4月7日 上午9:12:14<br>
     *
     * @param clazz
     * @param fields
     * @return
     * @author 
     */
    public static IncludePropertyFilter include(Class clazz, List<String> fields) {
        return new IncludePropertyFilter(clazz, fields);
    }

}
